package com.tbm.console.proj.service.impl;

import com.tbm.console.proj.entity.SystemRole;
import com.tbm.console.proj.repository.SystemRoleRepository;
import com.tbm.console.proj.service.SystemRoleService;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

@Service
public class SystemRoleServiceImpl implements SystemRoleService {

    @Autowired
    private SystemRoleRepository systemRoleRepository;


    @Override
    public List<SystemRole> findAll() {
        return findAll(new PageData());
    }

    @Override
    public List<SystemRole> findAll(Integer start, Integer length) {
        return findAll(new PageData(),start,length);
    }

    @Override
    public List<SystemRole> findAll(PageData pageData) {
        Specification<SystemRole> sp = this.getSystemRoleSpecification(pageData);
        List<SystemRole> all = systemRoleRepository.findAll(sp);
        return all;
    }



    @Override
    public List<SystemRole> findAll(PageData pageData, Integer start, Integer length) {
        Specification<SystemRole> sp = this.getSystemRoleSpecification(pageData);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        Page all = systemRoleRepository.findAll(sp, pageable);
        return all.getContent();
    }

    @Override
    public SystemRole findById(Integer id) {
        return systemRoleRepository.findById(id).get();
    }

    @Override
    public long count() {
        return count(new PageData());
    }

    @Override
    public long count(SystemRole systemRole) {
        Example<SystemRole> exa = Example.of(systemRole);
        return systemRoleRepository.count(exa);
    }

    @Override
    public long count(PageData pageData) {
        Specification<SystemRole> sp = this.getSystemRoleSpecification(pageData);
        return systemRoleRepository.count(sp);
    }

    @Override
    public boolean deleteById(Integer id) {
        systemRoleRepository.deleteById(id);
        return true;
    }

    @Override
    public boolean deleteByIds(Iterable<SystemRole> ids) {
        return false;
    }

    @Override
    public SystemRole save(SystemRole systemRole) {
        List<SystemRole> list = saveAll(Arrays.asList(systemRole));
        return list.size() == 0 ? new SystemRole() : list.get(0);
    }

    @Override
    public List<SystemRole> saveAll(List<SystemRole> systemRoles) {
        return systemRoleRepository.saveAll(systemRoles);
    }

    @Override
    public List<SystemRole> findAllById(Integer [] ids) {

        Iterable<Integer> it = new Iterable<Integer>() {
            @Override
            public Iterator<Integer> iterator() {
                return Arrays.asList(ids).iterator();
            }
        };
        return systemRoleRepository.findAllById(it);
    }


    //通过Specification来获取自定义的查询条件
    private Specification<SystemRole> getSystemRoleSpecification(PageData pageData){
        return new Specification() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList<Predicate>();
                if (null != pageData.get("id")) {
                    preList.add(criteriaBuilder.equal(root.get("id"), pageData.get("id").toString()));
                }
                if (null != pageData.get("menuId")) {
                    preList.add(criteriaBuilder.equal(root.get("menuId"), pageData.get("menuId").toString()));
                }
                if (null != pageData.get("parentId")) {
                    preList.add(criteriaBuilder.equal(root.get("parentId"), pageData.get("parentId").toString()));
                }
                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and(preList.toArray(p));
            }
        };
    }
}
